package com.cq.oa.web.sm.action;

import java.util.List;
import java.util.Map;

import oa.base.session.SessionManger;
import oa.base.util.Constants;
import oa.base.util.MD5Ecnrypt;
import oa.base.util.Page;
import oa.base.util.tree.TreeNode;
import oa.base.web.Action.BaseAction;

import com.cq.oa.domain.sm.User;
import com.cq.oa.service.sm.UserService;
import com.cq.oa.utils.NullUtil;
import com.cq.oa.utils.StringNumberUtils;

public class SmUserAction extends BaseAction<User> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3866290190273103555L;
	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	private Long deptId;
	
	private String deptCname;
	
	private String oldPwd;
	
	private String newPwd;
	
	private String sruePwd;
	
	private String searchParam;
	
	private Integer topNo;
	
	
	
	
	public Integer getTopNo() {
		return topNo;
	}

	public void setTopNo(Integer topNo) {
		this.topNo = topNo;
	}

	public String getSearchParam() {
		return searchParam;
	}

	public void setSearchParam(String searchParam) {
		this.searchParam = searchParam;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String getSruePwd() {
		return sruePwd;
	}

	public void setSruePwd(String sruePwd) {
		this.sruePwd = sruePwd;
	}

	public Long getDeptId() {
		return deptId;
	}

	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}

	public String getDeptCname() {
		return deptCname;
	}

	public void setDeptCname(String deptCname) {
		this.deptCname = deptCname;
	}

	public String findByPage() throws Exception {
		if(NullUtil.isNotNull(deptId)){
			filterMap.put("deptId", deptId);
			filterMap.put("org_sid", SessionManger.getOrgId());
		}else{
			filterMap.put("orgSid", SessionManger.getOrgId());
			filterMap.put(Constants.WHERE_LIKE+"username", entity.getUsername());
			filterMap.put(Constants.WHERE_LIKE+"cname", entity.getCname());
		}
		//Page<User> users = userService.findByPage(filterMap, linkMap,shortMap, super.getPageNo(),super.getPageSize());
		Page<User> users = userService.findByPageParm(filterMap, super.getPageNo(), super.getPageSize());
		this.renderJsonSuccess(jsonUtils.getExtJson(users, new String[] {}));
		return JSON;
	}

	@Override
	protected Class<User> getEntityClass() {
		return User.class;
	}

	@Override
	public void afterinit() {
		
	}

	@Override
	public void beforeInit() {
		
	}
	
	public String operate(){
		if(null!=entity.getSid()){
			entity = userService.findByParmkeys(entity.getSid());
		}
		return "operate";
	}
	
	public String save(){
		try {
			this.userService.saveUser(entity,deptId,deptCname);
		} catch (Exception e) {
			this.renderJsonError(e);
		}
		this.renderJsonSuccess();
		return JSON;
	}
	
	public String delete(){
		this.userService.delete(entity.getSid());
		this.renderJsonSuccess();
		return JSON;
	}
	/**
	 * 查询组织人员树
	 * @return
	 * @throws Exception
	 * lilang 
	 * 2013-10-10上午11:43:03
	 */
	public String findUserTree() throws Exception{
		List<TreeNode> nodes = this.userService.findOrgChild(id);
		return this.renderJsonSuccess(super.jsonUtils.getExtArrayJson(nodes));
	}
	/**
	 * 人员选择
	 * @return
	 * lilang 
	 * 2013-10-12上午11:28:25
	 */
	public String userSelect(){
		return "userSelect";
	}
	
	public String changePassword(){
		return "changePassword";
	}
	
	public String changePwdSave(){
		
		try {
			User user = this.userService.findByParmkeys(SessionManger.getUserId());
			if(!StringNumberUtils.password(user.getPassword(), oldPwd)){
				throw new Exception("原始密码错误!");
			}
			user.setPassword(MD5Ecnrypt.EncodeMD5Hex(newPwd));
			this.userService.updateSelect(user);
			this.renderJsonSuccess();
		} catch (Exception e) {
			this.renderJsonError(e);
		}
		
		return JSON;
	}
	/**
	 * 查询邮件 autocomplete
	 * @return
	 * lilang 
	 * 2013-10-17上午10:55:21
	 * @throws Exception 
	 */
	public String findEmail() throws Exception{
		List<Map<String,Object>> list = this.userService.findUserByEmail(searchParam, topNo);
		this.renderJsonSuccess(jsonUtils.getExtArrayJson(list));
		return JSON;
	}
	
	/**
	 * 查询 autocomplete
	 * @return
	 * lilang 
	 * 2013-10-17上午10:55:21
	 * @throws Exception 
	 */
	public String findByName() throws Exception{
		List<User> list = this.userService.findUserByName(searchParam, topNo);
		this.renderJsonSuccess(jsonUtils.getExtArrayJson(list));
		return JSON;
	}
	
	
}
